"""
单独下载Plotly库
"""
import requests
from pathlib import Path

# 尝试多个镜像源
plotly_sources = [
    'https://cdn.jsdelivr.net/npm/plotly.js-dist@2.27.0/plotly.min.js',
    'https://unpkg.com/plotly.js-dist@2.27.0/plotly.min.js',
    'https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.27.1/plotly.min.js',
    'https://cdn.plot.ly/plotly-2.27.0.min.js'
]

js_dir = Path('web_ui/static/js')
file_path = js_dir / 'plotly-2.27.0.min.js'

if file_path.exists():
    print("✅ plotly-2.27.0.min.js 已存在")
else:
    print("尝试下载 Plotly.js...")
    
    for i, url in enumerate(plotly_sources, 1):
        print(f"尝试源 {i}/{len(plotly_sources)}: {url}")
        try:
            response = requests.get(url, timeout=60)
            response.raise_for_status()
            
            with open(file_path, 'wb') as f:
                f.write(response.content)
            
            file_size = len(response.content) / 1024 / 1024
            print(f"✅ Plotly 下载成功 ({file_size:.2f} MB)")
            break
            
        except Exception as e:
            print(f"❌ 源 {i} 失败: {e}")
            if i == len(plotly_sources):
                print("\n所有源都失败了。你可以手动下载：")
                print("1. 访问 https://cdn.plot.ly/plotly-2.27.0.min.js")
                print(f"2. 保存文件到 {file_path.absolute()}")